package com.kili.recommend.data.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kili.recommend.data.config.ProjectEnum;
import com.kili.recommend.data.service.EventsService;
import com.kili.recommend.data.entity.Events;
import com.kili.recommend.data.mapper.EventsMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 事件汇总 服务实现类
 * </p>
 *
 * @author fenny
 * @since 2019-08-27
 */
@Service
public class EventsServiceImpl extends ServiceImpl<EventsMapper, Events> implements EventsService {

    @Override
    @DS("#projectEnum")
    public int put(ProjectEnum projectEnum, Events events) {
        return getInfo(events) != null ? baseMapper.addNum(events) : baseMapper.insert(events);
    }

    @Cacheable(value = "targetClass + methodName + #events.key", unless = "#result == null")
    public Events getInfo(Events events) {
        QueryWrapper<Events> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Events.EVENT, events.getEvent());
        queryWrapper.eq(Events.USER_ID, events.getUserId());
        queryWrapper.eq(Events.SKU_ID, events.getSkuId());
        return this.baseMapper.selectOne(queryWrapper);
    }

}
